#include"MessageBox.h"
MessageBox::MessageBox()
{}
/*
 * Best results with w = 45, h = 8
 */
MessageBox::MessageBox(char *title, int w, int h, char *msg) : Window(title, w, h)
{
	Message = (char *) malloc(50);
	strcpy(Message, msg);
	Ok.createButton("Ok", 8, 1);
}
MessageBox::~MessageBox()
{
	free(Message);
}
/****************************************************************************
 *  Name       : createMessageBox
 *  Parameters : Title of the MessageBox
 *               Width of the MessageBox (Ideally 45)
 *               Height of the MessageBox (Ideally 8)
 *               Message to be displayed
 *  Returns    : Nothing
 *  Purpose    : Reinitialize an existing object.
 ****************************************************************************/
void MessageBox::createMessageBox(char *title, int w, int h, char *msg)
{
	createWindow(title, w, h);
	Message = (char *) malloc(50);
	strcpy(Message, msg);
	Ok.createButton("Ok", 8, 1);
}
/****************************************************************************
 *  Name       : setMessage
 *  Parameters : Message to be set
 *  Returns    : Nothing
 *  Purpose    : To set a new message in an existing MessageBox object, and
 *               calculate the X position it must be displayed.
 ****************************************************************************/
void MessageBox::setMessage(char *msg)
{
	Rectangle w;
	if ( strlen(msg) > 50 )
		strcpy(Message, "Too long...");
	else
		strcpy(Message, msg);
	w = getWindowRect();
	Xpos = (w.left + w.right - strlen(Message)) / 2;
}
/****************************************************************************
 *  Name       : setXY
 *  Parameters : X position
 *               Y position
 *  Returns    : Nothing
 *  Purpose    : To set the positions of the MessageBox, Message and the
 *               Ok Button.
 ****************************************************************************/
void MessageBox::setXY(int x, int y)
{
	int off;
	Rectangle w;
	Window::setXY(x, y);
	off = (getWidth() - 8) / 2;
	Ok.setXY(x + off, y + 6);
	off = getWidth();
	if ( strlen(Message) > (off - 2 ) )
		strcpy(Message, "Too long...");
	w = getWindowRect();
	Xpos = (w.left + w.right - strlen(Message)) / 2;
	Ypos = y + 2;
}
/****************************************************************************
 *  Name       : getMessageBoxRect
 *  Parameters : None
 *  Returns    : Rectangle
 *  Purpose    : To get the Rectangle where the MessageBox is to be displayed.
 ****************************************************************************/
Rectangle MessageBox::getMessageBoxRect()
{
	return getWindowRect();
}
/****************************************************************************
 *  Name       : showMessageBox
 *  Parameters : None
 *  Returns    : Nothing
 *  Purpose    : To display the MessageBox.
 ****************************************************************************/
void MessageBox::showMessageBox()
{
	showWindow();
	putStr(Xpos, Ypos, Message, MSG_TXT, MSG_HOT);
	Ok.showButton();
}
/****************************************************************************
 *  Name       : handleMessageBox
 *  Parameters : None
 *  Returns    : Nothing
 *  Purpose    : Handles the MessageBox, since Button is the only active
 *               component calls the Button's handler.
 ****************************************************************************/
void MessageBox::handleMessageBox()
{
	Ok.handleButton();
}
// Class MessageBox ends
